import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'index',
    redirect:'/home',
    component: () =>import ( '../views/Index.vue'),
    children:[
      {
        path: '/home',
        name: 'home',
        component: () => import('../views/Home.vue'),
        children:[

        ]
      },
      {
        path: '/home/newcontent',
        name: 'newcontent',
        component: () => import('../components/newcontent'),
      },
      {
        path: '/home/searchlist',
        name: 'searchlist',
        component: () => import('../components/SearchList'),
      }

    ]
  },
  {
    path:'/adminindex',
    name:'adminindex',
    component:()=>import( '../views/admin/AdminIndex.vue'),
    redirect: 'homepage',
    children:[
      {
        path: '/homepage',
        name: 'homepage',
        component: () => import('../views/admin/administration/homepage.vue')
      },
      {
        path: '/edit',
        name: 'edit',
        component: () => import('../views/admin/administration/edit.vue')
      },
      {
        path: '/list',
        name: 'list',
        component: () => import('../views/admin/administration/list.vue')
      },
      {
        path: '/classification',
        name: 'classification',
        component: () => import('../views/admin/administration/classification.vue')
      },
      {
        path: '/examine',
        name: 'examine',
        component: () => import('../views/admin/administration/examine.vue')
      },
      {
        path: '/img',
        name: 'img',
        component: () => import('../views/admin/administration/img.vue')
      },
      {
        path: '/hotsearch',
        name: 'hotsearch',
        component: () => import('../views/admin/administration/hotsearch.vue')
      },
      {
        path: '/link',
        name: 'link',
        component: () => import('../views/admin/administration/link.vue')
      },
      {
        path: '/personal',
        name: 'personal',
        component: () => import('../views/admin/administration/personal.vue')
      },
      {
        path: '/user',
        name: 'user',
        component: () => import('../views/admin/administration/user.vue')
      },
      {
        path: '/comment',
        name: 'comment',
        component: () => import('../views/admin/administration/comment.vue')
      }
    ]
  }

]
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

const whiteList = ['/home','/home/newcontent','/home/searchlist']
router.beforeEach((to, from, next) => {
  const user = JSON.parse(localStorage.getItem("user"))
  let token = "";
  let isAdmin = false;
  if (user && user.role) {
    isAdmin = user.role;
  }
  if (user && user.token) {
    token = user.token;
  }
  console.log(token)
  if (token) {
    if (isAdmin==true) {
      next()
    }else {
      if (whiteList.includes(to.path)) {
        next()
      } else {
        // 如果其他页面请强制拦截并跳转到登录页面 x
        next('/home')
      }
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      // 如果其他页面请强制拦截并跳转到登录页面
      next('/home')
    }
  }
})
export default router
